package com.xgkj.diyiketang.util.xiangce;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.R;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;

public class BitmapCache extends Activity {

	public Handler h = new Handler();
	public final String TAG = getClass().getSimpleName();
	private HashMap<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();
	private Context mContext;

	public BitmapCache(Context context) {
		mContext = context;
	}

	public BitmapCache(){}

	public void put(String path, Bitmap bmp) {
		if (!TextUtils.isEmpty(path) && bmp != null) {
			imageCache.put(path, new SoftReference<Bitmap>(bmp));
		}
	}

	public Bitmap get(String path){
		if (!TextUtils.isEmpty(path)){
			if (imageCache.containsKey(path)) {
				SoftReference<Bitmap> reference = imageCache.get(path);
				Bitmap bmp = reference.get();
				if (bmp != null) {
					return bmp;
				}
			}
		}
		return null;
	}

	public void displayBmp(final ImageView iv, final String thumbPath,
			final String sourcePath, final ImageCallback callback) {
		Bitmap defaultImageBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zhanweitu);
		iv.setImageBitmap(defaultImageBitmap);

		if (TextUtils.isEmpty(thumbPath) && TextUtils.isEmpty(sourcePath)) {
			return;
		}

		final String path;
		final boolean isThumbPath;
		if (!TextUtils.isEmpty(thumbPath)) {
			path = thumbPath;
			isThumbPath = true;
		} else if (!TextUtils.isEmpty(sourcePath)) {
			path = sourcePath;
			isThumbPath = false;
		} else {
			return;
		}

		if (imageCache.containsKey(path)) {
			SoftReference<Bitmap> reference = imageCache.get(path);
			Bitmap bmp = reference.get();
			if (bmp != null) {
				if (callback != null) {
					callback.imageLoad(iv, bmp, sourcePath);
				}
				iv.setImageBitmap(bmp);
				return;
			}
		}

		new Thread() {
			Bitmap thumb;

			public void run() {

				try {
					if (isThumbPath) {
						File thumbFile = new File(thumbPath);
						if (thumbFile.exists()) {
							thumb = revitionImageSize(thumbPath);
							if (thumb == null) {
								thumb = revitionImageSize(sourcePath);
							}
						}else {
							thumb = revitionImageSize(sourcePath);
						}
					} else {
						thumb = revitionImageSize(sourcePath);											
					}
				} catch (Exception e) {	
					
				}
				if (thumb == null) {
					thumb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zhanweitu);
				}
				put(path, thumb);

				if (callback != null) {
					h.post(new Runnable() {
						@Override
						public void run() {
							callback.imageLoad(iv, thumb, sourcePath);
						}
					});
				}
			}
		}.start();

	}

	public Bitmap revitionImageSize(String path) throws IOException {

		File file = new File(path);
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in, null, options);
		in.close();
		int jiaodu = readPictureDegree(file.getAbsolutePath());
		int i = 0;
		Bitmap bitmap = null;
		while (true) {
			if ((options.outWidth >> i <= 256)
					&& (options.outHeight >> i <= 256)) {
				in = new BufferedInputStream(
						new FileInputStream(new File(path)));
				options.inSampleSize = (int) Math.pow(2.0D, i);
				options.inJustDecodeBounds = false;
				if (jiaodu != 0) {
					Bitmap midbitmap = BitmapFactory.decodeStream(in, null, options);
					bitmap = rotaingImageView(jiaodu, midbitmap);
					midbitmap.recycle();
				}else {
					bitmap = BitmapFactory.decodeStream(in, null, options);
				}
				break;
			}
			i += 1;
		}
		return bitmap;
	}

	public  Bitmap rotaingImageView(int angle , Bitmap bitmap) {
		//旋转图片 动作
		Matrix matrix = new Matrix();;
		matrix.postRotate(angle);
		System.out.println("angle2=" + angle);
		// 创建新的图片
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
				bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		bitmap.recycle();
		return resizedBitmap;
	}

	public  int readPictureDegree(String path) {
		int degree  = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
				case ExifInterface.ORIENTATION_ROTATE_90:
					degree = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180:
					degree = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270:
					degree = 270;
					break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

	public interface ImageCallback {
		public void imageLoad(ImageView imageView, Bitmap bitmap,
							  Object... params);
	}
}
